﻿using System;
using System.Reflection.Metadata;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using Consul;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;
using Document = B.S.DataBase.MyProject.Domain.Procurements.Document;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    ///采购订单的明细
    /// </summary>
    public class DetailsPurchaseorderCommandHander : IRequestHandler<DetailsPurchaseorderCommand, APIResult<List<SupplierDto>>>
    {

        /// <summary>
        /// 采购订单
        /// </summary>
        private readonly IBaseRepository<Purchaseorder> baseRepository;
        /// <summary>
        /// 物料信息表
        /// </summary>
        private readonly IBaseRepository<Materials_Info> WaterbaseRepository;

        /// <summary>
        /// 业务类型
        /// </summary>
        private readonly IBaseRepository<Document> Docrepository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        /// <param name="waterbaseRepository"></param>
        /// <param name="docrepository"></param>
        public DetailsPurchaseorderCommandHander(IBaseRepository<Purchaseorder> baseRepository, IBaseRepository<Materials_Info> waterbaseRepository, IBaseRepository<Document> docrepository)
        {
            this.baseRepository = baseRepository;
            WaterbaseRepository = waterbaseRepository;
            Docrepository = docrepository;
        }




        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<SupplierDto>>> Handle(DetailsPurchaseorderCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SupplierDto>> result = new APIResult<List<SupplierDto>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = from a in WaterbaseRepository.GetAll().Result
                       join b in baseRepository.GetAll().Result
                       on a.MaterialsInfoid equals b.PurchaseOrderId
                       join c in Docrepository.GetAll().Result
                       on b.Documentid equals c.Documentid
                       select new SupplierDto
                       {
                           PurchaseOrderId = b.PurchaseOrderId,
                           DocumentCode = b.DocumentCode,
                           DocumentStatus = b.DocumentStatus,
                           BusinessStatus = b.BusinessStatus,
                           BusinessType =b.BusinessType,
                           
                           PurchasingDe = b.PurchasingDe,
                           Documentid = b.Documentid,
                           Purchasingid = b.Purchasingid,//采购部门
                           Operator =b.Operator,
                           Auditor = b.Auditor,
                           AuditDate=b.AuditDate,
                           MaterialData =b.MaterialData,
                           MaterialRire =b.MaterialRire,
                           Supid =b.Supid,
                           ArrivalDate =b.ArrivalDate,
                           PurchaseContract =b.PurchaseContract,
                           RemarksInfor =b.RemarksInfor,
                           DocumentidName =c.DocumentidName,
                           MaterialsInfoid =a.MaterialsInfoid,
                           SaleCode =a.SaleCode,
                           Document = a.Document,
                           DocumentStatuss =a.DocumentStatus,
                           Materialid = a.Materialid,
                           MaterialName =a.MaterialName,
                           ModelSper =a.ModelSper,
                           ModePrimary = a.ModePrimary,
                           ModeDate= a.ModeDate,
                           ModeQuantity = a.ModeQuantity,
                           ModeUnitPrice = a.ModeUnitPrice,
                           ModeAmount = a.ModeAmount,
                           ModeReceived = a.ModeReceived,
                           ModePurchase = a.ModePurchase,
                       };

            if (!string.IsNullOrEmpty(request.DocumentCode))
            {
                list = list.Where(X => X.DocumentCode == request.DocumentCode);
            }
            if (!string.IsNullOrEmpty(request.BusinessStatus))
            {
                list = list.Where(X => X.BusinessStatus == request.BusinessStatus);
            }
            if (!string.IsNullOrEmpty(request.BusinessType))
            {
                list = list.Where(X => X.BusinessType == request.BusinessType);
            }

            // 添加需求日期范围查询条件
            if (request.ModeDateStart.HasValue && request.ModeDateEnd.HasValue)
            {
                list = list.Where(x => x.DocumentStatus >= request.ModeDateStart && x.ModeDate <= request.ModeDateEnd);
            }
            else if (request.ModeDateStart.HasValue)
            {
                list = list.Where(x => x.DocumentStatus >= request.ModeDateStart);
            }
            else if (request.ModeDateEnd.HasValue)
            {
                list = list.Where(x => x.DocumentStatus <= request.ModeDateEnd);
            }

            result.data = list.ToList();
            return Task.FromResult(result);

        }
    }
}
